package com.borderbears.frontend
{
	import com.baseoneonline.flash.utils.logger.Logger;
	import com.borderbears.frontend.camera.CameraSystem;
	import com.borderbears.frontend.util.Const;
	import com.borderbears.frontend.util.Cursor;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getQualifiedClassName;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.render.IRenderEngine;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.utils.PrecisionMode;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	public class SceneView extends Sprite
	{
		
		private var log:Logger = Logger.getInstance(getQualifiedClassName(this));

		private var viewPort:Viewport3D;
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var renderer:IRenderEngine;
		
		private var model:DAE;
			
		private var camSystem:CameraSystem = new CameraSystem();

		private var zoomedIn:Boolean = false;
		
		
		public function SceneView()
		{
			viewPort = new Viewport3D(300, 
				300, true, true, true, true);
			addChild(viewPort);
			scene = new Scene3D();
			
			scene.addChild(camSystem);
			camera = camSystem.getCamera();
			
			var rend:QuadrantRenderEngine = new QuadrantRenderEngine(QuadrantRenderEngine.CORRECT_Z_FILTER);
			
			
			
			//renderer = new BasicRenderEngine();
			renderer = rend;
			

			model = new DAE();
			
			model.addEventListener(FileLoadEvent.LOAD_COMPLETE, function(e:FileLoadEvent):void {
				scene.addChild(model);
				
				log.info("Collada model loaded.");
				init();
			}, false, 0, false);
			model.addEventListener(FileLoadEvent.LOAD_ERROR, function(e:FileLoadEvent):void {
				log.warn("Error loading collada: "+Const.COLLADA_FILE_ROOM);
			}, false, 0, true);
			
			
			model.load(Const.COLLADA_FILE_ROOM, null, true);
			log.info("Loading "+Const.COLLADA_FILE_ROOM);
			addEventListener(MouseEvent.MOUSE_UP, onClick);
			
			
		}
		
		private function onClick(e:MouseEvent):void {
			
			
		}
		
		public function nextCamera():void {
			camSystem.nextCamera();
		}
		
		private function toggleCamera(camNr:int):void {
			if (!zoomedIn) {
				camSystem.setCamera(camNr);
			} else {
				zoomOut();
			}
			zoomedIn = !zoomedIn;
		}
	
		
		private function init():void {
			
			camSystem.findCamNodes(model);
			
			var matList:MaterialsList = model.materials;
			
			for each(var o:Object in matList.materialsByName) {
				if (o is BitmapMaterial) {
					var b:BitmapMaterial = o as BitmapMaterial;
					b.precisionMode = PrecisionMode.ORIGINAL;
					b.smooth = false;
					b.interactive = true;
					b.precise = true;
				}
			}

			
			addAction(Const.OBJECT_SUITCASE, function():void {
				toggleCamera(Const.CAMERA_SUITCASE);
			}, Cursor.FORWARD);
			addAction(Const.OBJECT_TELEPHONE, function():void {
				toggleCamera(Const.CAMERA_TELEPHONE);
			}, Cursor.FORWARD);
			addAction(Const.OBJECT_WINDOW, function():void {
				toggleCamera(Const.CAMERA_WINDOW);
			}, Cursor.FORWARD);
			
			
			
		}
		
		private function zoomOut():void {
			camSystem.setCamera(0);
		}
		
		public function addAction(objectName:String, callBack:Function, cursor:Bitmap=null):void {
			var d:DisplayObject3D = model.getChildByName(objectName, true);
			if (null != d) {
				d.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, 
					function(e:InteractiveScene3DEvent):void {
						log.info("Clicked: '"+objectName+"'");
						callBack();
					}
				);
				if (null != cursor) {
					d.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,
						function(e:InteractiveScene3DEvent):void {
							Cursor.getCursor().setCursor(cursor);
						}
					);
					d.addEventListener(InteractiveScene3DEvent.OBJECT_OUT,
						function(e:InteractiveScene3DEvent):void {
							Cursor.getCursor().restoreCursor();
						}
					);
				}
			}
		}
		
		
		
		public function update(t:Number = 0):void {
			
			var totalWidth:Number = stage.stageWidth;
			var totalHeight:Number = stage.stageHeight;
			
			var x:Number = ((mouseX/totalWidth)-.5)*2;
			var y:Number = ((mouseY/totalHeight)-.5)*2;
			
			camSystem.tweakAim(x,y);
			
			camSystem.update(t);
			renderer.renderScene(scene, camera, viewPort);
		}
		
		
	}
}